import {createRouter, createWebHistory} from 'vue-router'
import MainLayout from "@/layout/MainLayout.vue";
import SupplierManage from "@/views/SupplierManage.vue";
import GoodsList from "@/views/GoodsList.vue";
import CategoryManage from "@/views/CategoryManage.vue";
import BrandManage from "@/views/BrandManage.vue";
import InventoryInbound from "@/views/InventoryInbound.vue";
import InventoryOut from "@/views/InventoryOut.vue";
import InventoryTransfer from "@/views/InventoryTransfer.vue";
import InventoryAlert from "@/views/InventoryAlert.vue";
import OrderList from "@/views/OrderList.vue";
import ReturnManage from "@/views/ReturnManage.vue";
import SalesAnalysis from "@/views/SalesAnalysis.vue";
import FinishGoods from "@/views/FinishGoods.vue";
import UserLogin from "@/views/UserLogin.vue";
import ProductsShop from "@/views/ProductsShop.vue";
import OrderDetail from "@/views/OrderDetail.vue";

const routes = [
    {
        path: '/login',
        component: () => import('@/views/Login.vue')
    },
    {
      path:'/user',
        component:ProductsShop


    },
    {
        path: '/user/orderDetail',
        component:OrderDetail
    },
    {
        path: '/',
        redirect: '/products/goodsList',
        component: MainLayout,
        meta: {requiresAuth: true},
        children: [
            {
                path: 'suppliers',
                component: SupplierManage,
                meta: {
                    title: '供应商管理',
                    requiresAuth: true
                }
            },
            {
                path: 'products',
                children: [
                    {
                        path: 'goodsList',
                        component: GoodsList,
                        meta: {
                            title: '商品管理',
                            requiresAuth: true
                        }
                    },
                    {
                        path: 'categoryList',
                        component: CategoryManage,
                        meta: {
                            title: '分类管理',
                            requiresAuth: true
                        }
                    },
                    {
                        path: 'brands',
                        component: BrandManage,
                        meta: {
                            title: '品牌管理',
                            requiresAuth: true
                        }
                    }

                ]
            }, {
                path: 'inventory',
                children: [
                    {
                        path: 'inbound',
                        component: InventoryInbound,
                        meta: {
                            title: '入库管理',
                            requiresAuth: true
                        }
                    },
                    {
                        path: 'outbound',
                        component: InventoryOut,
                        meta: {
                            title: '出库管理',
                            requiresAuth: true
                        }
                    },

                    {
                        path: 'alert',
                        component: InventoryAlert,
                        meta: {
                            title: '库存预警',
                            requiresAuth: true
                        }
                    }


                ]

            },
            {
                path: 'orders',
                children: [

                    {
                        path: 'list',
                        component: OrderList,
                        meta: {
                            title: '订单管理',
                            requiresAuth: true
                        }
                    }, {
                        path: 'return',
                        component: ReturnManage,
                        meta: {
                            title: '退货管理',
                            requiresAuth: true
                        }
                    },
                    {
                        path: 'analysis',
                        component: SalesAnalysis,
                        meta: {
                            title: '销售分析',
                            requiresAuth: true
                        }
                    }

                ]


            }, {
                path: 'finish',
                children: [
                    {
                        path: 'goods',
                        component: FinishGoods,
                        meta: {
                            title: '成品管理',
                            requiresAuth: true
                        }
                    }

                ]
            }
            // 仓库管理路由
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        component: () => import('@/views/404.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to) => {
    const token = localStorage.getItem('token')
    if (to.meta.requiresAuth && !token) {
        return '/login'
    }
})

export default router